/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-07-20
 * Time: 9:51
 */
public class Test5 {
    private static Object lock1 = new Object();

    private static Object lock2 = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized(lock1) {
                System.out.println("线程1拿到lock1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized(lock2) {
                    System.out.println("线程1拿到lock2");
                }
            }
        });

        Thread t2 = new Thread(() -> {
            synchronized(lock2) {
                System.out.println("线程2拿到lock2");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized(lock1) {
                    System.out.println("线程2拿到lock1");
                }
            }

        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("线程结束");
    }

}
